﻿using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Data.OracleClient;
using System.Collections;
using System.Collections.Generic;
namespace INTEX.DataBase.Tools
{
    public partial class INTEXBL
    {
        public object CreateAB05(string ITXDBVERSION, DataSet db)
        {
            DataSet SReturn = new DataSet();
            DataSet SResult = new DataSet();
            DataTable tbNABPOS = new DataTable();
            List<DataRow> list=null;
            string KRS = "", LFD = "", POS = "", ABPNEUBEN, ABPNEUDAT;
            string VKPKRS = "  ", VKPLFD = "000000",  VKPPOS = "000";
            decimal ABPSOLLMENGE1 = 0, ABPSOLLMENGE2 = 0, ABPSOLLMENGE3 = 0, ABPSOLLMENGE4 = 0, ABPSOLLMENGE5 = 0, ABPSOLLMENGE6 = 0, ABPSOLLMENGE7 = 0,
                ABPSOLLMENGE8 = 0;
            string filter = "";
            DataRow[] rows;            
            DataTable tbMU = db.Tables["ICC_ABAX"];
            DataTable tbABP = db.Tables["ABPOS_EXT"];
            int idx = 0;
            var qABP = from DataRow row in tbABP.AsEnumerable()
                       where row["ABPAUFART"].ToString() == "VA"
                       select row;            
            foreach (DataRow dwABP in qABP)
            {
                list = new List<DataRow>();
                MANDANT = dwABP["ABPMANDANT"].ToString();
                ABPNEUBEN = dwABP["ABPNEUBEN"].ToString();
                ABPNEUDAT = dwABP["ABPNEUDAT"].ToString();
                string ABPSTARTDAT = dwABP["ABPSTARTDAT"].ToString();
                string ABPTERMDAT = dwABP["ABPTERMDAT"].ToString(); 
                KRS = dwABP["ABPAUFKRS"].ToString();
                if (idx == 0)
                {
                    LFD = GetABP(ITXDBVERSION, KRS, ref db, dwABP);
                    list.Add(CreateABK(dwABP, ITXDBVERSION));
                }
                if (LFD == "" || LFD == "000000")
                {
                    throw new Exception("未能成功获取生产单号码:" + KRS);
                }
                idx++;
                POS = Right("000" + dwABP["ABPAUFPOS"].ToString(), 3);
                ABPSOLLMENGE1 = Convert.ToDecimal(dwABP["ABPSOLLMENGE1"].ToString());
                ABPSOLLMENGE2 = Convert.ToDecimal(dwABP["ABPSOLLMENGE2"].ToString());
                ABPSOLLMENGE3 = Convert.ToDecimal(dwABP["ABPSOLLMENGE3"].ToString());
                ABPSOLLMENGE4 = Convert.ToDecimal(dwABP["ABPSOLLMENGE4"].ToString());
                ABPSOLLMENGE5 = Convert.ToDecimal(dwABP["ABPSOLLMENGE5"].ToString());
                ABPSOLLMENGE6 = Convert.ToDecimal(dwABP["ABPSOLLMENGE6"].ToString());
                ABPSOLLMENGE7 = Convert.ToDecimal(dwABP["ABPSOLLMENGE7"].ToString());
                ABPSOLLMENGE8 = Convert.ToDecimal(dwABP["ABPSOLLMENGE8"].ToString());
                #region=====ABPOS=========
                EventDispatcher.Instance.RaiseStatus("转换ABPOS" + KRS + LFD, StatusEventType.RunningProgress);
                DataRow dwABPOS = INTEXDBC.Convert2ABPOS(dwABP, ITXDBVERSION);
                dwABPOS["ABPMANDANT"] = MANDANT;
                dwABPOS["ABPAUFKRS"] = KRS;
                dwABPOS["ABPAUFLFD"] = LFD;
                dwABPOS["ABPAUFPOS"] = POS;
                dwABPOS["ABPDIVARTKZ"] = "N";
                dwABPOS["ABPAUFART"] = dwABP["ABPAUFART"];
                dwABPOS["ABPWAKZ"] = dwABP["ABPWAKZ"];
                dwABPOS["ABPART"] = dwABP["ABPART"];
                dwABPOS["ABPVAR"] = dwABP["ABPVAR"];
                dwABPOS["ABPFARBE"] = dwABP["ABPFARBE"];
                dwABPOS["ABPBESCHICH"] = dwABP["ABPBESCHICH"];
                dwABPOS["ABPZLO"] = dwABP["ABPZLO"];
                dwABPOS["ABPZLB"] = dwABP["ABPZLB"];
                dwABPOS["ABPSOLLMENGE1"] = dwABP["ABPSOLLMENGE1"];
                dwABPOS["ABPSOLLMENGE2"] = dwABP["ABPSOLLMENGE2"];
                dwABPOS["ABPSOLLMENGE3"] = dwABP["ABPSOLLMENGE3"];
                dwABPOS["ABPSOLLMENGE4"] = dwABP["ABPSOLLMENGE4"];
                dwABPOS["ABPSOLLMENGE5"] = dwABP["ABPSOLLMENGE5"];
                dwABPOS["ABPSOLLMENGE6"] = dwABP["ABPSOLLMENGE6"];
                dwABPOS["ABPSOLLMENGE7"] = dwABP["ABPSOLLMENGE7"];
                dwABPOS["ABPSOLLMENGE8"] = dwABP["ABPSOLLMENGE8"];
                dwABPOS["ABPANRKR"] = dwABP["ABPANRKR"];
                dwABPOS["ABPALFDNR"] = dwABP["ABPALFDNR"];
                dwABPOS["ABPSTDLAE"] = dwABP["ABPSTDLAE"];
                dwABPOS["ABPMGV"] = dwABP["ABPMGV"];
                dwABPOS["ABPMGB"] = dwABP["ABPMGB"];
                dwABPOS["ABPTMCOUNT"] = 4;
                dwABPOS["ABPZUTART"] = 1;
                dwABPOS["ABPFA"] = 1;
                dwABPOS["ABPGA"] = 100;
                dwABPOS["ABPQA"] = 100;
                dwABPOS["ABPAESTAT"] = " ";
                dwABPOS["ABPREGEL"] = "00000007";
                dwABPOS["ABPDIVARTKZ"] = "N";
                dwABPOS["ABPSME"] = 2;
                dwABPOS["ABPVFAUFKRS"] = 1;
                dwABPOS["ABPAUFST"] = 8;
                dwABPOS["ABPSZUSATZKZ1"] = "F";
                dwABPOS["ABPANZSTK"] = 0;
                dwABPOS["ABPZUGETGES"] = 0;
                dwABPOS["ABPSTKLG"] = 0;
                dwABPOS["ABPWBSTA"] = "68";
                dwABPOS["ABPSZUSATZKZ1"] = "F";
                dwABPOS["ABPSZUSATZKZ2"] = " ";
                dwABPOS["ABPKETTAUFT"] = " "; 
                //仅在执行坯布预留时存在
                dwABPOS["ABPRESMGE"] = dwABP["ABPRESMGE"];
                dwABPOS["ABPRESKG"] = dwABP["ABPRESKG"];
                dwABPOS["ABPRESSTK"] = dwABP["ABPRESSTK"];

                dwABPOS["ABPLIEFDAT"] = dwABP["ABPLIEFDAT"];
                dwABPOS["ABPVTDATE"] = dwABP["ABPVTDATE"];
                dwABPOS["ABPSTARTDAT"] = dwABP["ABPSTARTDAT"];
                dwABPOS["ABPENDDAT"] = dwABP["ABPENDDAT"];
                dwABPOS["ABPSTARTDATA"] = dwABP["ABPSTARTDATA"];
                dwABPOS["ABPENDDATA"] = dwABP["ABPENDDATA"];
                dwABPOS["ABPANRKR"] = dwABP["ABPANRKR"];
                dwABPOS["ABPALFDNR"] = dwABP["ABPALFDNR"];
                dwABPOS["ABPLIEFDATN"] = dwABP["ABPLIEFDATN"];
                dwABPOS["ABPPOSTYP"] = dwABP["ABPPOSTYP"];
                dwABPOS["ABPTERMDAT"] = dwABP["ABPTERMDAT"];
                dwABPOS["ABPSTARTDATN"] = dwABP["ABPSTARTDATN"];
                dwABPOS["ABPNEUBEN"] = dwABP["ABPNEUBEN"];
                dwABPOS["ABPNEUDAT"] = dwABP["ABPNEUDAT"];

                list.Add(dwABPOS);

                #endregion
                #region=====ABPR=========
                list.Add(CreateABR(dwABPOS, ITXDBVERSION));
                #endregion
                #region====ABAUFM=========
                EventDispatcher.Instance.RaiseStatus("转换ABAUFM" + KRS + LFD, StatusEventType.RunningProgress);
                DataRow dwProd = INTEXDBC.Convert2ABAUFM(null, ITXDBVERSION, false);
                dwProd["ABAMANDANT"] = MANDANT;
                dwProd["ABAAUFTYP"] = "V";
                dwProd["ABAAUFKRS"] = KRS;
                dwProd["ABAAUFLFD"] = LFD;
                dwProd["ABAAUFPOS"] = POS;
                dwProd["ABAAPOS"] = "0001";
                dwProd["ABASGESGEW"] = ABPSOLLMENGE5 * 10;
                dwProd["ABASME"] = "2";
                dwProd["ABALEAPOS"] = "2";
                dwProd["ABASGESLG"] = ABPSOLLMENGE2 * 10;
                dwProd["ABASGESGEW"] = ABPSOLLMENGE6 * 10;
                dwProd["ABARSTSLG"] = 0;
                dwProd["ABARSTSGEW"] = 0;
                dwProd["ABAVAKZ"] = "J";
                dwProd["ABALIEFKOMP"] = " ";
                dwProd["ABANEUBEN"] = ABPNEUBEN;
                dwProd["ABANEUDAT"] = ABPNEUDAT;
                list.Add(dwProd);

                if (tbMU != null)
                {
                    filter = "ABAAUFKRS='" + KRS + "' AND ABAAUFLFD='" + LFD + "' AND ABAAUFPOS='" + POS + "'";
                    rows = tbMU.Select(filter);
                    foreach (DataRow dwReq in rows)
                    {
                        NewReservaton(dwProd, dwReq, ITXDBVERSION, "AB05MU01",ref list);
                    }
                }
                #endregion

                #region=====ABFOLGE=========
                ConsoldateFlow(dwABPOS, ITXDBVERSION, "AB05", ref list);
                #endregion
                #region=====WDECKZ=========
                EventDispatcher.Instance.RaiseStatus("转换WDECKZ", StatusEventType.RunningProgress);
                DataRow dwWDECK = INTEXDBC.Convert2WDECK(null, "JLT", false);
                dwWDECK["WDMANDANT"] = MANDANT;
                dwWDECK["WDAUFKRS"] = KRS;
                dwWDECK["WDAUFLFD"] = LFD;
                dwWDECK["WDAUFPOS"] = POS;
                dwWDECK["WDUPOS"] = "01";
                dwWDECK["WDTYP"] = "B";
                dwWDECK["WDDAT"] = ABPTERMDAT;
                dwWDECK["WDWAKZ"] = dwABP["ABPWAKZ"];
                dwWDECK["WDART"] = dwABP["ABPART"];
                dwWDECK["WDFARBE"] = dwABP["ABPFARBE"];
                dwWDECK["WDBESCHICH"] = dwABP["ABPBESCHICH"];
                
                dwWDECK["WDPRART"] = dwABP["ABPART"];
                dwWDECK["WDPRVAR"] = dwABP["ABPVAR"];

                dwWDECK["WDNEUDAT"] = ABPNEUDAT;
                dwWDECK["WDNEUBEN"] = ABPNEUBEN;
                list.Add(dwWDECK);
                #endregion
                #region=====WDECKB=========
                filter = "ABAWAKZ='R' AND ABAVKAUFTYP='V' AND ABAVKAUFKRS='" + KRS + "' AND ABAVKAUFLFD='" + LFD + "'AND ABAVKAUFPOS='" + POS + "'";
                rows = tbMU.Select(filter);

                foreach (DataRow row in rows)
                {
                    dwWDECK = INTEXDBC.Convert2WDECK(null, "JLT", false);
                    dwWDECK["WDMANDANT"] = MANDANT;
                    dwWDECK["WDAUFKRS"] = KRS;
                    dwWDECK["WDAUFLFD"] = LFD;
                    dwWDECK["WDAUFPOS"] = POS;
                    dwWDECK["WDUPOS"] = "01";
                    dwWDECK["WDTYP"] = "Z";

                    dwWDECK["WDNEUBEN"] = ABPNEUBEN;
                    dwWDECK["WDNEUDAT"] = ABPNEUDAT;
                    dwWDECK["WDWAKZ"] = row["ABAWAKZ"];
                    dwWDECK["WDART"] = row["ABAART"];
                    dwWDECK["WDVAR"] = row["ABAVAR"];
                    dwWDECK["WDFARBE"] = row["ABAFARBE"];
                    dwWDECK["WDBESCHICH"] = row["ABAFBESCHICH"];
                    dwWDECK["WDVAR"] = row["ABAVAR"];
                    dwWDECK["WDDAT"] = ABPSTARTDAT;
                    dwWDECK["WDBEDARF"] = ABPSOLLMENGE1;
                    dwWDECK["WDRESERV"] = ABPSOLLMENGE1;
                    dwWDECK["WDVARFR"] = "J";
                    dwWDECK["WDBEDST"] = "M";
                    dwWDECK["WDPRART"] = row["ABAART"];
                    dwWDECK["WDPRVAR"] = row["ABAVAR"];
                    list.Add(dwWDECK);
                }
                #endregion
                #region===ICC_ABAX=============
                if (tbMU != null)
                {
                    /*
                    filter = "ABAAUFKRS='" + KRS + "' AND ABAAUFLFD='" + LFD + "' AND ABAAUFPOS='" + POS + "'";
                    rows = tbMU.Select(filter);
                    if (rows.Length > 0)
                    {
                        foreach (DataRow row in rows)
                        {
                            DataRow dwABAX = INTEXDBC.Convert2ICC_ABAX(row, app.company, true);
                            dwABAX["ABAID"] = 0;
                            list.Add(dwABAX);
                        }
                    }
                    */
                }
                #endregion
                SaveAB(ITXDBVERSION, list);
                #region=======UpdateVKPOS======
                if (tbMU != null)
                {
                    filter = "ABAAUFKRS='" + KRS + "' AND ABAAUFLFD='" + LFD + "' AND ABAAUFPOS='" + POS + "'";
                    rows = tbMU.Select(filter);
                    foreach (DataRow dwReq in rows)
                    {
                        filter = "VKPAUFKRS='" + VKPKRS + "' AND VKPAUFLFD='" + VKPLFD + "' AND VKPAUFPOS='" + VKPPOS + "'";
                        rows = db.Tables["VKPOS"].Select(filter);
                        foreach (DataRow dwVKPOS in rows)
                        {
                            decimal ABASANZSTK = Convert.ToDecimal(dwReq["ABASANZSTK"].ToString());
                            decimal VKPLAEBIS = Convert.ToDecimal(dwReq["ABASSTKLG"].ToString());
                            dwVKPOS["VKPPRODST"] = "M";
                            dwVKPOS["VKPRESERV"] = dwReq["ABARESMG"];//*0.9144
                            dwVKPOS["VKPRESERVT1"] = ABASANZSTK * 100;
                            dwVKPOS["VKPRESERVT2"] = dwReq["ABARESMG"];//*0/1
                            dwVKPOS["VKPRESERVT3"] = dwABPOS["ABPSOLLMENGE4"];
                            dwVKPOS["VKPRESERVT4"] = dwABPOS["ABPSOLLMENGE6"];
                            INTEXDBC.UpdateVKPOS(dwVKPOS, "JLT", 5);
                        }
                    }
                }
                #endregion
            }
            return list;
        }
    }
}

